package com.uenpay.im.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qyx.android.weight.utils.QyxWeightDensityUtils;
import com.uenpay.im.R;


public class LocationView extends RelativeLayout {
	ImageView image;
	TextView address;

	public LocationView(Context context) {
		super(context);
		init(context);
	}

	public LocationView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public LocationView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		image = new ImageView(context);
		LayoutParams params = new LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		image.setLayoutParams(params);
		image.setImageResource(R.drawable.default_location_attachment_bg);
		image.setScaleType(ImageView.ScaleType.FIT_XY);

		address = new TextView(context);
		LayoutParams textParams = new LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		textParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		address.setLayoutParams(textParams);
		address.setGravity(Gravity.CENTER);
		address.setBackgroundColor(context.getResources().getColor(
				R.color.location_text_background));
		address.setTextColor(Color.WHITE);
		address.setPadding(10, 10, 10, 10);

		addView(image);
		addView(address);
	}

	public void setAddress(String text) {
		if (TextUtils.isEmpty(text)) {
			address.setVisibility(INVISIBLE);
		} else {
			address.setVisibility(VISIBLE);
		}
		address.setText(text);
		Rect bounds = new Rect();
		Paint paint = address.getPaint();
		int length = 0;
		if(TextUtils.isEmpty(text)){
			text = getResources().getString(R.string.unknown_loaction);
		}else{
			length = text.length();
		}
		paint.getTextBounds(text, 0, length, bounds);
		LayoutParams params = new LayoutParams(
				LayoutParams.MATCH_PARENT, QyxWeightDensityUtils.dp2px(
						getContext(), 100));
		image.setLayoutParams(params);
		LayoutParams textParams = new LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		textParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		address.setLayoutParams(textParams);
	}
}
